﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using System.Data.Objects;
using Common;

namespace BestBuy.BuyerViews
{
    public partial class ManagerAuctions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.RoleID != (int)RoleType.Buyer)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadBookmark(user);
                LoadCreateAuctions(user.ID);
                LoadEndAuctions(user);
            }
        }
        
        private void LoadBookmark(User user)
        {
            User userBookmark = UserDAL.GetAllBookmark(user.ID);
            gvBookmark.DataSource = userBookmark.FollowAuctions;
            gvBookmark.DataBind();
        }

        private void LoadCreateAuctions(int userID)
        {
            gvCreateAuctions.DataSource = UserDAL.GetCreateAucByBuyerID(userID);
            gvCreateAuctions.DataBind();
        }

        private void LoadEndAuctions(User user)
        {
            var EndAuctions = UserDAL.GetEndAucByBuyerID(user.ID);
            gvEndAuctions.DataSource = EndAuctions;
            gvEndAuctions.DataBind();
        }

        private string GetAuctionStatus(int status)
        {
            string result = "";
            switch (status)
            {
                case (int)AuctionStatus.Active:
                    result = AuctionStatus.Active.ToString();
                    break;
                case (int)AuctionStatus.Delete:
                    result = AuctionStatus.Delete.ToString();
                    break;
                case (int)AuctionStatus.Endtime:
                    result = AuctionStatus.Endtime.ToString();
                    break;
                case (int)AuctionStatus.Inactive:
                    result = AuctionStatus.Inactive.ToString();
                    break;
                case (int)AuctionStatus.Finish:
                    result = AuctionStatus.Finish.ToString();
                    break;
                case (int)AuctionStatus.Watting:
                    result = AuctionStatus.Watting.ToString();
                    break;

            }

            return result;
        }

        private string Auc_Tran_Status(int status)
        {
            string result = "";
            if (status == (int)AuctionTransactionStatus.Fail)
                result = AuctionTransactionStatus.Fail.ToString();
            else if (status == (int)AuctionTransactionStatus.Success)
                result = AuctionTransactionStatus.Success.ToString();
            else if (status == (int)AuctionTransactionStatus.Waitting)
                result = AuctionTransactionStatus.Waitting.ToString();

            return result;
        }

        protected void gvBookmark_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataAccessLayer.Auction auc = (DataAccessLayer.Auction)DataBinder.Eval(e.Row.DataItem, "Auction");
                if (auc != null)
                {
                    HyperLink link = e.Row.FindControl("AuctionLink") as HyperLink;
                    Label endtime = e.Row.FindControl("EndTimeLable") as Label;
                    Label lbstatus = e.Row.FindControl("StatusLable_bookmark") as Label;
                    if (link != null)
                    {
                        link.Text = auc.Title;
                        link.NavigateUrl = string.Format("~/UserViews/SellerBid.aspx?AuctionID={0}", auc.ID);
                    }
                    if (endtime != null)
                    {
                        endtime.Text = auc.EndTime.ToString();
                    }
                    if (lbstatus != null)
                    {
                        lbstatus.Text = GetAuctionStatus(auc.Status);
                    }
                }
            }
            
        }

        protected void gvCreateAuctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int status = (int)DataBinder.Eval(e.Row.DataItem, "Status");
                Label lbstatus = e.Row.FindControl("StatusLable_create") as Label;
                if (lbstatus != null)
                {
                    lbstatus.Text = GetAuctionStatus(status);
                }
                if (status == (int)AuctionStatus.Delete)
                {
                    Button del = e.Row.FindControl("DeleteButton") as Button;
                    if (del != null)
                    {
                        del.Visible = false;
                    }
                }
            }
        }

        protected void gvEndAuctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataAccessLayer.Auction auc = (DataAccessLayer.Auction)DataBinder.Eval(e.Row.DataItem, "Auction");
                DataAccessLayer.User seller = (DataAccessLayer.User)DataBinder.Eval(e.Row.DataItem, "User");
                int status = (int)DataBinder.Eval(e.Row.DataItem,"Status");
                if (auc != null)
                {
                    HyperLink Auctionlink = e.Row.FindControl("AuctionLink_End") as HyperLink;
                    HyperLink Feedbacklink = e.Row.FindControl("FeedbackLink") as HyperLink;
                    Label lbstatus = e.Row.FindControl("Statuslable_End") as Label;
                    if (Auctionlink != null && Feedbacklink != null && lbstatus != null)
                    {
                        Auctionlink.Text = auc.Title;
                        Auctionlink.NavigateUrl = string.Format("~/UserViews/SellerBid.aspx?AuctionID={0}", auc.ID);
                    }
                    if (Feedbacklink != null)
                    {
                        Feedbacklink.NavigateUrl = string.Format("~/UserViews/Feedback.aspx?AuctionID={0}", auc.ID);
                    }
                    if (lbstatus != null)
                    {
                        lbstatus.Text = Auc_Tran_Status(status);
                    }
                }
                //if (seller != null)
                //{
                //    Label lbseller = e.Row.FindControl("SellerLable_End") as Label;
                //    if (lbseller != null)
                //    {
                //        lbseller.Text = seller.Username;
                //    }
                //}
            }
        }

        protected void DeleteButtion_Click(object sender, EventArgs e)
        {
            Button senderButton = sender as Button;
            int id = Int32.Parse(senderButton.Attributes["ObjectId"]);
            // Delete object here
            AuctionDAL.DeleteAuction(id);
        }

        protected void gvCreateAuctions_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}